﻿using Project.API.Models.GoodModels;
using Project.BLL.GoodsBll;
using Project.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Remoting.Messaging;
using System.Security.Cryptography;
using System.Web.Http;
using System.Web.Security;
using System.Data.Entity.Core.Objects;
using System.Drawing;
using System.Web.UI;
using Project.BLL.UsersInfoBll;
using Project.BLL.MemberBll;

namespace Project.API.Controllers.GoodsControllers
{
    public class OrdersController : ApiController
    {
        OrdersBll bll = new OrdersBll();

        // GET api/<controller>  列表显示
        public ResultPageList<OrdersDto> Get(int page, int size, string name, DateTime? StartTime = null, DateTime? EndTime = null)
        {
            //处理为null
            name = name ?? "";
            ResultPageList<OrdersDto> result = new ResultPageList<OrdersDto>();
            var data = bll.Query().Where(x => x.Users.userName.Contains(name));   //模糊查询

            // 时间范围 筛选
            if (StartTime != null && EndTime != null)
            {

                data = data.Where(x => x.CreateDate >= StartTime && x.CreateDate <= EndTime);
            }

            //赋值总数
            result.count = data.Count();

            //分页
            var linq = data.OrderBy(x => x.Did).Skip((page - 1) * size).Take(size).Select(x => new OrdersDto
            {
                Did = x.Did,
                UserId = x.UserId,
                Gid = x.Gid,
                CreateDate = x.CreateDate,
                OrderStatus = x.OrderStatus,
                TotalAmount = x.TotalAmount,
                OrderNumber = x.OrderNumber,
                Notes = x.Notes,
                userName = x.Users.userName,
                Goodsname = x.Goods.name,
                SpvList = (from s in x.OrdersSpec
                           select new OrdersSpecDto
                           {
                               Specvalue = s.GoodsPropertyValue.GoodsSpecValue.value,
                           }).ToList()
                        ,
                Images = x.Goods.images
            }).ToList();

            result.data = linq;
            result.code = data.Count() > 0 ? 1 : 0;
            return result;
        }

        // GET api/<controller>/5
        public List<OrdersDto> Get(int id)
        {
            var data = bll.Query();
            var linq = data.OrderByDescending(x => x.CreateDate).Select(x => new OrdersDto
            {
                Did = x.Did,
                UserId = x.UserId,
                Gid = x.Gid,
                CreateDate = x.CreateDate,
                OrderStatus = x.OrderStatus,
                TotalAmount = x.TotalAmount,
                OrderNumber = x.OrderNumber,
                Notes = x.Notes,
                userName = x.Users.userName,
                Goodsname = x.Goods.name,
                Address=x.Address,
                SpvList = (from s in x.OrdersSpec
                           select new OrdersSpecDto
                           {
                               Specvalue = s.GoodsPropertyValue.GoodsSpecValue.value,
                           }).ToList()
                      ,
                Images = x.Goods.images
            }).ToList();

            return linq.ToList();
        }

        // POST api/<controller>  添加订单
        public Result Post([FromBody] List<OrdersEditDto> value)
        {
            List<Orders> orders = new List<Orders>();

            Entity entity = new Entity();

            OrdersSpecBll ordersSpecBll = new OrdersSpecBll();
            int bol = 0;
            Result result = new Result(); // 提前创建Result对象

            // 循环处理每个订单
            foreach (var orderDto in value)
            {
                // 调用添加流水号存储过程
                var no = new ObjectParameter("no", typeof(int));
                entity.f_Proc_Order_No("Orders", "Did", "LX", no);

                Orders order = new Orders(); // 创建新的 Orders 对象

                order.Did = no.Value + "";
                order.UserId = orderDto.UserId;
                order.Gid = orderDto.Gid;
                order.CreateDate = DateTime.Now;
                order.OrderStatus = "制作中";
                order.TotalAmount = orderDto.TotalAmount;
                order.OrderNumber = orderDto.OrderNumber;
                order.Notes = orderDto.Notes;
                order.Address = orderDto.Address;

                bol = bll.Add(order) ? 1 : 0;

                if (orderDto.OrdersSpec != null && orderDto.OrdersSpec.Any())
                {
                    // 添加商品规格
                    bol = ordersSpecBll.Add(orderDto.OrdersSpec.Select(x => new OrdersSpec
                    {
                        Did = order.Did,
                        Gpvid = x.Gpvid
                    }).ToList()) ? 1 : 0;
                }

                // 添加用户积分:  
             
                var userId = orderDto.UserId; 
                var userdata = new UsersInfoBll().Query().FirstOrDefault(x => x.user_id == userId);

                if (userdata != null)
                {
                    var memberdata = new MemberBll().Query().FirstOrDefault(x => x.user_info.user_id == userId && x.Mid != 1);
                    //如果用户是 高级会员 或 至尊会员 积分翻倍
                    if (memberdata != null)
                    {
                        userdata.points += 40;
                    }
                    else
                    {
                        userdata.points += 20;
                    }

                    var userbol = new UsersInfoBll().Update(userdata); // 更新用户积分
                }
                else
                {
                    bol = 0;  //添加不成功
                    result.code = bol;
                    return result;
                }
            }

            // 批量添加订单和更新用户积分完成
            result.code = bol; // 最终结果
            return result;
        }


        // PUT api/<controller>/5  修改
        public Result Put([FromBody] OrdersEditDto value)
        {
            Orders orders = new Orders();
            orders.Did = value.Did;
            orders.UserId = value.UserId;
            orders.Gid = value.Gid;
            orders.CreateDate = value.CreateDate;
            orders.OrderStatus = value.OrderStatus;
            orders.TotalAmount = value.TotalAmount;
            orders.OrderNumber = value.OrderNumber;
            orders.Notes = value.Notes;


            Result result = new Result();
            result.code = bll.Update(orders) ? 1 : 0;
            return result;
        }

        // DELETE api/<controller>/5   订单不可以被删除 
        public void Delete(int id)
        {
        }
    }
}